package com.meeting.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meeting.entity.User;
import com.meeting.exception.BusinessException;
import com.meeting.model.dto.user.UserModifyRequest;
import com.meeting.model.dto.user.UserQueryRequest;
import com.meeting.model.dto.user.UserRegisterRequest;
import com.meeting.model.enums.UserRole;
import com.meeting.model.enums.UserStatusEnum;
import com.meeting.model.vo.UserVO;
import com.meeting.service.UserService;
import com.meeting.mapper.UserMapper;
import com.meeting.utils.JwtUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

/**
* @author 25843
* @description 针对表【user(用户表（员工和管理员）)】的数据库操作Service实现
* @createDate 2025-04-19 12:54:48
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    /**
     * 盐值，混淆密码
     */
    public static final String SALT = "meeting";

    @Override
    public String adminLogin(String username, String password) {
        String encryptPassword = DigestUtils.md5DigestAsHex((SALT + password).getBytes());
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username);
        wrapper.eq(User::getPassword, encryptPassword);
        User user = this.baseMapper.selectOne(wrapper);
        if (user == null) {
            throw new BusinessException(400, "用户名或密码错误");
        }
        if (!user.getRole().equals(UserRole.ADMIN.getCode())) {
            throw new BusinessException(403, "无管理员权限");
        }
        return JwtUtil.generateToken(user);
    }

    @Override
    public String employeeLogin(String username, String password) {
        String encryptPassword = DigestUtils.md5DigestAsHex((SALT + password).getBytes());
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username);
        wrapper.eq(User::getPassword, encryptPassword);
        User user = this.baseMapper.selectOne(wrapper);
        if (user == null) {
            throw new BusinessException(400, "用户名或密码错误");
        }
        if (!user.getRole().equals(UserRole.EMPLOYEE.getCode())) {
            throw new BusinessException(403, "无员工权限");
        }
        if (user.getStatus().equals(UserStatusEnum.FROZEN.getCode())) {
            throw new BusinessException(403, "账号被冻结，请联系管理员");
        }
        if (user.getStatus().equals(UserStatusEnum.PENDING_REVIEW.getCode())) {
            throw new BusinessException(403, "账号审核中，请联系管理员");
        }
        if (user.getStatus().equals(UserStatusEnum.REJECTED.getCode())) {
            throw new BusinessException(403, "账号审核拒绝，请联系管理员");
        }
        return JwtUtil.generateToken(user);
    }

    @Override
    public Wrapper<User> getQueryWrapper(UserQueryRequest queryRequest) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        if (queryRequest == null) {
            return queryWrapper;
        }
        Long id = queryRequest.getId();
        String username = queryRequest.getUsername();
        String realName = queryRequest.getRealName();
        Integer role = queryRequest.getRole();
        Integer status = queryRequest.getStatus();

        queryWrapper.eq(ObjectUtils.isNotEmpty(id) && !id.equals(1L), User::getId, id);
        queryWrapper.like(StringUtils.isNotBlank(username), User::getUsername, username);
        queryWrapper.like(StringUtils.isNotBlank(realName), User::getRealName, realName);
        queryWrapper.eq(ObjectUtils.isNotEmpty(role), User::getRole, role);
        queryWrapper.eq(ObjectUtils.isNotEmpty(status), User::getStatus, status);
        queryWrapper.ne(User::getId, 1L); // 拿不到管理员的信息
        return queryWrapper;
    }

    @Override
    public Boolean modifyUserStatus(UserModifyRequest userModifyRequest) {
        if (userModifyRequest == null || userModifyRequest.getId() == 1L) {
            throw new BusinessException(400, "参数错误");
        }
        User user = new User();
        BeanUtils.copyProperties(userModifyRequest, user);
        int i = this.baseMapper.updateById(user);
        return i > 0;
    }

    @Override
    public Boolean register(UserRegisterRequest userRegisterRequest) {
        String username = userRegisterRequest.getUsername();
        String realName = userRegisterRequest.getRealName();
        String password = userRegisterRequest.getPassword();

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username);
        User one = this.getOne(wrapper);
        if (one != null) {
            throw new BusinessException(400, "用户名已存在");
        }
        String encryptPassword = DigestUtils.md5DigestAsHex((SALT + password).getBytes());
        User user = new User();
        user.setUsername(username);
        user.setRealName(realName);
        user.setPassword(encryptPassword);
        user.setRole(UserRole.EMPLOYEE.getCode());
        user.setStatus(UserStatusEnum.PENDING_REVIEW.getCode());
        return this.save(user);
    }
}




